package com.park.user.service.park.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.park.comm.ParkEnum;
import com.park.comm.ResultEnum;
import com.park.dao.mapper.order.ParkOrderInfoMapper;
import com.park.dao.mapper.park.ParkInfoMapper;
import com.park.dao.mapper.user.UserLicensePlateMapper;
import com.park.dao.mapper.user.UserMapper;
import com.park.entity.car.CarLocation;
import com.park.entity.licenseplate.LicenseParkVo;
import com.park.entity.order.ParkOrderInfo;
import com.park.entity.park.ParkInfo;
import com.park.entity.user.UserInfo;
import com.park.entity.user.UserLicensePlate;
import com.park.exception.CommException;
import com.park.user.service.park.ParkInfoService;
import com.park.utils.distance.LongLatDistanceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Administrator
 * @title: ParkInfoServiceImpl
 * @projectName park
 * @description: 停车场信息业务处理类
 * @date 2020/5/222:06
 */
@Service
public class ParkInfoServiceImpl implements ParkInfoService {

    @Autowired
    private ParkInfoMapper parkInfoMapper;

    @Autowired
    private UserLicensePlateMapper userLicensePlateMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ParkOrderInfoMapper parkOrderInfoMapper;

    /**
     　　* @description: 查询是否可以停车
     　　* @param [carLocation]
     　　* @return
     　　* @throws
     　　* @author ZhangHaiWen
     　　* @version 2.0
     　　* @date 2020/5/2 22:07
     　　*/
    @Override
    public LicenseParkVo getCanPark(CarLocation carLocation) {
        //0-未绑定车牌  1-(绑定车牌了)不可以停车，且无支付订单无进行中的订单  2-(绑定车牌了)可以停车 3-(绑定车牌了)进行中订单 4-(绑定车牌了)待支付订单
        Integer orderStatusVo = -1;
        String licensePlateVo = null;
        String orderPhoneNumberVo = null;
        Long orderStarStampVo = null;
        Long orderEndStampVo = null;
        BigDecimal orderTotalVo =  null;
        Integer orderIdVo = null;
        Integer parkIdVo = null;
        String parkNameVo = null;
        Long inTimeStampVo = null;
        Long outTimeStampVo = null;

        Integer userId = carLocation.getUserId();
        //判断是是否绑定车牌
        UserLicensePlate userLicensePlate = userLicensePlateMapper.selectByUserId(userId);
        if(ObjectUtil.isNotEmpty(userLicensePlate)){
            orderStatusVo = 1;
            licensePlateVo = userLicensePlate.getLicensePlate();
            //查询未结束的订单 不考虑3的状态
            ParkOrderInfo orderByUserLicense = parkOrderInfoMapper.getOrderByUserLicense(userLicensePlate.getLicensePlate());
            if(ObjectUtil.isNotEmpty(orderByUserLicense)){

                orderPhoneNumberVo = orderByUserLicense.getOrderPhoneNumber();
                orderStarStampVo = orderByUserLicense.getInTimeStamp();
                inTimeStampVo = orderByUserLicense.getInTimeStamp();

                orderIdVo = orderByUserLicense.getId();
                parkIdVo = orderByUserLicense.getParkId();
                parkNameVo = orderByUserLicense.getParkName();

                // 0-未支付 1-已经支付
                Integer orderPayStatus = orderByUserLicense.getOrderPayStatus();
                // 0-未开始停车 1-进行中 2-已结束停车 3-结束  已经结束停车，需要去支付，支付完成，这笔订单才算完成
                Integer orderStatus = orderByUserLicense.getOrderStatus();
                // 查询是否有进行中的订单
                if(orderPayStatus.equals(ParkEnum.ORDER_PAY_STATUS_UNPAY.status) && orderStatus.equals(ParkEnum.PARK_STATUS_ING.status)){
                    //订单进行中
                    orderStatusVo = 3;
                }

                if(orderPayStatus.equals(ParkEnum.ORDER_PAY_STATUS_UNPAY.status) && orderStatus.equals(ParkEnum.PARK_STATUS_END.status) ){
                    //待支付的订单
                    orderStatusVo = 4;
                    orderEndStampVo = orderByUserLicense.getOutTimeStamp();
                    outTimeStampVo = orderEndStampVo;
                    //待支付金额
                    orderTotalVo = orderByUserLicense.getOrderTotal();

                }
            }
            if(orderStatusVo == 1){
                // 判断是否可以停车
                Double carLat = carLocation.getCarLat();
                Double carLong = carLocation.getCarLong();
                if(carLat!=null && carLong!=null){
                    boolean flag = false;
                    /*List<ParkInfo> parkInfos = parkInfoMapper.selectAll();
                    ParkInfo canParkInfo = new ParkInfo();
                    //判断是否存在可以停车的距离
                    for (ParkInfo parkInfo : parkInfos) {
                        Double parkLat = parkInfo.getParkLat();
                        Double parkLong = parkInfo.getParkLong();
                        double distance = LongLatDistanceUtil.distance(carLat, carLong, parkLat, parkLong);
                        if(distance<=1000){
                            flag = true;
                            canParkInfo = parkInfo;
                            break;
                        }
                    }*/
                    // 可以停车
                    orderStatusVo = 2;
                    parkIdVo = -1;
                     /*parkIdVo = canParkInfo.getId();
                        parkNameVo = canParkInfo.getParkName();*/
                }
            }
        }else{
            orderStatusVo = 0;
        }
        //数据组装
        LicenseParkVo licenseParkVo = new LicenseParkVo();
        licenseParkVo.setOrderStatus(orderStatusVo);
        licenseParkVo.setLicensePlate(licensePlateVo);
        licenseParkVo.setOrderPhoneNumber(orderPhoneNumberVo);
        licenseParkVo.setOrderStarStamp(orderStarStampVo);
        licenseParkVo.setOrderEndStamp(orderEndStampVo);
        licenseParkVo.setOrderTotal(orderTotalVo);
        licenseParkVo.setOrderId(orderIdVo);
        licenseParkVo.setParkId(parkIdVo);
        licenseParkVo.setParkName(parkNameVo);
        licenseParkVo.setInTimeStamp(inTimeStampVo);
        licenseParkVo.setOutTimeStamp(outTimeStampVo);
        return licenseParkVo;
    }

    /**
     　　* @description: 立即停车按钮 点击
     　　* @param [carLocation]
     　　* @return
     　　* @throws
     　　* @author ZhangHaiWen
     　　* @version 2.0
     　　* @date 2020/5/3 9:23
     　　*/
    @Override
    public void doPark(CarLocation carLocation) {
        Integer userId = carLocation.getUserId();
        Integer parkId = null;
        //获取当前位置的定位
        Double carLat = carLocation.getCarLat();
        Double carLong = carLocation.getCarLong();
        if(ObjectUtil.isEmpty(carLat) || ObjectUtil.isEmpty(carLong) ){
            throw new CommException(ResultEnum.CANNOT_STOP_ERROR);
        }
        //根据用户id查询车牌
        UserLicensePlate userLicensePlate = userLicensePlateMapper.selectByUserId(userId);
        if(ObjectUtil.isNotEmpty(userLicensePlate)){
            //查询当前车牌是否存在 进行中的订单
            String licensePlate = userLicensePlate.getLicensePlate();
            ParkOrderInfo parkOrderInfoIng = parkOrderInfoMapper.getIngPlate(licensePlate);
            if(ObjectUtil.isNotEmpty(parkOrderInfoIng)){
                throw new CommException(ResultEnum.CAR_PLATE_ORDERING_ERROR);
            }
            boolean flag = false;
            List<ParkInfo> parkInfos = parkInfoMapper.selectAll();
            //判断是否存在可以停车的距离
            for (ParkInfo parkInfo : parkInfos) {
                Double parkLat = parkInfo.getParkLat();
                Double parkLong = parkInfo.getParkLong();
                double distance = LongLatDistanceUtil.distance(carLat, carLong, parkLat, parkLong);
                if(distance<=1000){
                    flag = true;
                    parkId = parkInfo.getId();
                    break;
                }
            }
            if(!flag){
                throw new CommException(ResultEnum.CANNOT_STOP_ERROR);
            }
            UserInfo userInfo = userMapper.selectByPrimaryKey(userId);
            //进行下单
            ParkInfo parkInfo = parkInfoMapper.selectByPrimaryKey(parkId);
            String orderNo = IdUtil.simpleUUID();

            ParkOrderInfo parkOrderInfo = new ParkOrderInfo();
            parkOrderInfo.setOrderNo(orderNo);
            // 0 - 未开始停车
            parkOrderInfo.setOrderStatus(ParkEnum.PARK_STATUS_INIT.status);
            parkOrderInfo.setOrderLicense(userLicensePlate.getLicensePlate());
            parkOrderInfo.setOrderPayStatus(ParkEnum.ORDER_PAY_STATUS_UNPAY.status);
            long stamp = System.currentTimeMillis();
            parkOrderInfo.setOrderStarStamp(stamp);
            parkOrderInfo.setUserId(userId);
            parkOrderInfo.setOrderPhoneNumber(userInfo.getPhoneNumber());
            parkOrderInfo.setAddTimeStamp(stamp);
            parkOrderInfo.setParkId(parkId);
            parkOrderInfo.setTimeStamp(stamp);
            parkOrderInfo.setOrderPrice(parkInfo.getParkHourFee());
            parkOrderInfo.setParkName(parkInfo.getParkName());
            parkOrderInfoMapper.insert(parkOrderInfo);
        }else{
            throw new CommException(ResultEnum.CAR_PLATE_BINDING_ERROR);
        }

    }

}
